package module03.Question2;

/*编程获取两个指定字符串中的最大相同子串*/
import java.util.Scanner;

public class FindSameString {

    public static void main(String[] args) {

        //1.输入要比较的字符串
        System.out.println("请输入两个字符串，用空格隔开：");
        Scanner sc = new Scanner(System.in);
        String str1 = sc.next();
        String str2 = sc.next();

        //2.比较两个字符串的长短
        String miniStr;
        String maxStr;
        if(str1.length() >= str2.length()){
            miniStr = str2;
            maxStr = str1;
        } else {
            miniStr = str1;
            maxStr = str2;
        }

        //3.用较短的字符串的子串逐步去与较长的比较
        int mini = miniStr.length();
        int max = maxStr.length();
        //逐步缩短子串的长度
        for(int i = 0; i < mini; i++){
            for(int x = 0, y = mini - i; y <= mini; x++,y++){
                String subStr = miniStr.substring(x,y);
                //与长字符串进行比较
                if(maxStr.contains(subStr)){
                    System.out.println("最大子串为" + subStr);
                    return;
                }
            }

        }

    }
}